package com.example.personalcenter.Tool;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OKHttpTool {
    public static String TOKEN;
    public static String URL = "http://124.93.196.45:10001/";
    public static String IP = "http://124.93.196.45:10001";

    public static final String GET = "get";
    public static final String POST = "post";
    public static final String PUT = "put";
    public static final String DELETE = "delete";

    private static OkHttpClient okHttpClient;
    private static Gson gson;

    public static void init(){
        okHttpClient = null;
        gson = null;
        okHttpClient = new OkHttpClient();
        gson = new Gson();
    }

    public static void deNetRequest(String url,
                                    String method,
                                    Class classOfT,
                                    NetCallback ncb,
                                    String... jsonParames){
        Request.Builder rb = new Request.Builder();
        if (!TextUtils.isEmpty(url)){
            rb.url(URL + url);
        }
        if (TextUtils.isEmpty(TOKEN)) {
            Log.v("OKHttpTool","token=null");
        }else {
            if (url.contains("login")){
                Log.v("OKHttpTool","login-no-add-token");
            }else {
                rb.addHeader("Authorization",TOKEN);
            }
        }

        if (GET.equals(method)){
            rb.get();
        }else if (POST.equals(method) && jsonParames.length > 0){
            rb.post(RequestBody.create(jsonParames[0], MediaType.parse("application/json; charset=utf-8")));
        }else if (PUT.equals(method) && jsonParames.length > 0){
            rb.post(RequestBody.create(jsonParames[0], MediaType.parse("application/json; charset=utf-8")));
        }else if (DELETE.equals(method) && jsonParames.length > 0){
            rb.delete();
        }

        if (okHttpClient == null || gson == null){
            init();
        }
        Request request = rb.build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.d("OKHttpTool","onFailure");
                Log.d("OKHttpTool",e.getMessage());
                ncb.onFailure("Request failure");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json = response.body().string();
                Log.d("OKHttpTool","Result:"+json);
                Object obj = null;
                try {
                    obj = gson.fromJson(json,classOfT);
                    ncb.onSuccess(obj);
                } catch (JsonSyntaxException e) {
                    ncb.onFailure("Request failure.Json parse exception");
                }
            }
        });

    }

    public interface NetCallback {
        void onSuccess(Object obj);

        void onFailure(String msg);
    }

}
